﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Songhay.Tests
{
    using Songhay.Models;
    using Songhay.Xml;

    /// <summary>
    /// Summary description for XmlTest
    /// </summary>
    [TestClass]
    public class XmlTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        public class OneTwoThree
        {
            public string One { get; set; }
            public string Two { get; set; }
            public string Three { get; set; }
        }

        /// <summary>
        /// Should serialize the object.
        /// </summary>
        /// <remarks>
        ///     “System.Xml.XmlSerializer”
        ///     [http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp]
        /// </remarks>
        [TestMethod]
        public void ShouldSerializeObject()
        {
            var data = new OneTwoThree
            {
                One = "uno",
                Two = "dos",
                Three = "tres"
            };

            var elementTest = new Action<XDocument, string>((xd, elementName) =>
            {
                Assert.AreEqual(elementName,
                    xd.Root.Element(elementName).Name,
                    "The expected element name is not here.");
            });

            var serializer = new XmlSerializer(typeof(OneTwoThree));
            MemoryStream stream = new MemoryStream();
            try
            {
                using(var writer = new XmlTextWriter(stream, Encoding.UTF8))
                {
                    serializer.Serialize(stream, data);
                    var xml = Encoding.UTF8.GetString(stream.ToArray());

                    var xd = XDocument.Parse(xml);
                    Assert.AreEqual("OneTwoThree", xd.Root.Name);
                    elementTest.Invoke(xd, "One");
                    elementTest.Invoke(xd, "Two");
                    elementTest.Invoke(xd, "Three");
                }
            }
            finally
            {
                if(stream != null) stream.Dispose();
            }
        }

        /// <summary>
        /// Should read folder of XHTML files and write index.
        /// </summary>
        [TestMethod]
        public void ShouldReadFolderOfXhtmlFilesAndWriteIndex()
        {
            //var indexFileName = "CssIndex.xml";
            //var indexTitle = "CSS Samples";
            //var publicRoot = "~/Content/Samples/css/";
            //var pathToFolder = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Web.Mvc.ServerIndex\Content\Samples\css\";

            var indexFileName = "JQueryIndex.xml";
            var indexTitle = "JQuery Samples";
            var publicRoot = "~/Content/Samples/jQuery/";
            var pathToFolder = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Web.Mvc.ServerIndex\Content\Samples\jquery\";

            var pathToOutput = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Web.Mvc.ServerIndex\App_Data\Samples\";

            XhtmlDocumentUtility.WriteDocumentIndex(indexFileName, indexTitle,
                publicRoot, pathToFolder, pathToOutput);
        }

        [TestMethod]
        public void ShouldReadIndex()
        {
            var pathToInput = @"E:\~shares\dataRoot\Visual Studio 2010\Projects\Songhay.Web.Mvc.ServerIndex\App_Data\Samples\CssIndex.xml";
            var instance = XmlUtility.GetInstance<XhtmlDocuments>(pathToInput) as XhtmlDocuments;
            Assert.IsNotNull(instance);
        }
    }
}
